document.addEventListener("mouseup", function (e) {
  var selection = window.getSelection();
  var text = selection.toString();
  if (text.length > 0) {
    // 获取选区的范围
    var range = selection.getRangeAt(0);
    var rect = range.getBoundingClientRect();

    // 定位悬浮窗
    $("#myTooltip").css({
      top: rect.top + window.scrollY - 80, // 上方40px
      left:
        rect.left +
        window.scrollX +
        rect.width / 2 -
        $("#myTooltip").outerWidth(),
      display: "flex",
    });
  } else {
    $("#myTooltip").hide();
  }
});

// 点击页面其他地方隐藏悬浮窗
document.addEventListener("mousedown", function (e) {
  if (!$(e.target).closest("#myTooltip").length) {
    $("#myTooltip").hide();
  }
});
